// $codepro.audit.disable numericLiterals, spellCheckIdentifiers, spellCheckComments
/**
 * Package gui
 */
package teamrocket.gui; // $codepro.audit.disable packagePrefixNamingConvention

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import teamrocket.domain.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

/**
 * This is the gui for logging in to the system.
 * @author James Zhang, Juan Llanes, Stephen Tapper, Will Barr
 * @version 1.0
 */
public class LoginFrame extends JFrame {

	/**
	 * Field contentPane.
	 */
	private final JPanel contentPane;

	/**
	 * Field tbUsername.
	 */
	private final JTextField tbUsername;
	
	/**
	 * Field tbPassword.
	 */
	private final JTextField tbPassword;
	
	/**
	 * Field lblFeedback.
	 */
	private final JLabel lblFeedback;

	/**
	 * Launch the application.
	 * @param args String[]
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 264, 282);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JLabel lblCredPrompt = new JLabel("Enter your credentials to login");
		lblCredPrompt.setBounds(40, 39, 186, 14);
		contentPane.add(lblCredPrompt);
		
		final JLabel lblUsernamePrompt = new JLabel("Username:");
		lblUsernamePrompt.setBounds(50, 74, 70, 14);
		contentPane.add(lblUsernamePrompt);
		
		final JLabel lblPasswordPrompt = new JLabel("Password:");
		lblPasswordPrompt.setBounds(50, 105, 70, 14);
		contentPane.add(lblPasswordPrompt);
		
		tbUsername = new JTextField();
		tbUsername.setBounds(119, 71, 86, 20);
		contentPane.add(tbUsername);
		tbUsername.setColumns(10);
		
		tbPassword = new JTextField();
		tbPassword.setBounds(119, 102, 86, 20);
		contentPane.add(tbPassword);
		tbPassword.setColumns(10);

		lblFeedback = new JLabel("feedback");
		lblFeedback.setBounds(26, 189, 200, 44);
		contentPane.add(lblFeedback);
		
		final JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login(tbUsername.getText(), tbPassword.getText());
			}
		});
		btnLogin.setBounds(83, 132, 89, 23);
		contentPane.add(btnLogin);
		
	}
	
	/**
	 * Method login.
	 * @param username String
	 * @param password String
	 */
	private void login(String username, String password) {
		User u = null; // $codepro.audit.disable questionableName -->
		try {
			u = User.createFilledUser(username);
			final int result = u.doLogin(password);
			if(result == 0) {
				lblFeedback.setText("Login successful for user: " + tbUsername.getText());
				
				//open a new window
				final MainFrame mainFrame = new MainFrame(u);
				mainFrame.setVisible(true);
				
				setVisible(false);
				
			}
			else if(result == 1) {
				lblFeedback.setText("Login failed for user: " + tbUsername.getText());
			}
			else if( result == 2) {
				lblFeedback.setText(tbUsername.getText() + " is locked.");
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
